
package com.be.beadmin.product.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.design.mapper.ProductBomOverviewMapper;
import com.be.beadmin.design.repository.ProductBomOverviewRepository;
import com.be.beadmin.design.repository.ProductBomRepository;
import com.be.beadmin.design.repository.ProductDrawingInfoRepository;
import com.be.beadmin.product.dto.input.BatchPlanInforDetailDto;
import com.be.beadmin.product.dto.input.BatchPlanInforDto;
import com.be.beadmin.product.dto.output.BatchPlanInforEx;
import com.be.beadmin.product.entity.BatchPlanInfor;
import com.be.beadmin.product.entity.BatchPlanInfor_;
import com.be.beadmin.product.mapper.BatchPlanInforMapper;
import com.be.beadmin.product.mapper.BatchPlanInforMapperEx;
import com.be.beadmin.product.repository.BatchPlanInforRepository;
import com.be.beadmin.product.service.BatchPlanInforService;
import com.be.beadmin.project.entity.BatchPlan;
import com.be.beadmin.project.entity.BatchProduct;
import com.be.beadmin.project.entity.Info;
import com.be.beadmin.project.repository.BatchPlanRepository;
import com.be.beadmin.project.repository.BatchProductRepository;
import com.be.beadmin.project.repository.InfoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author zjm
* @date 2023-03-19
**/
@Service
@RequiredArgsConstructor
public class BatchPlanInforServiceImpl implements BatchPlanInforService {

    @Autowired
    private  BatchPlanInforRepository batchPlanInforRepository;
    @Autowired
    private  BatchPlanInforMapper batchPlanInforMapper;

    @Autowired
    private  BatchPlanInforMapperEx batchPlanInforExMapper;

//    @Autowired
//    private InfoRepository infoRepository;

    @Override
    public Page<BatchPlanInforEx> getBatchPlanInforByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<BatchPlanInfor> page = batchPlanInforRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(BatchPlanInfor_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(BatchPlanInfor_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<BatchPlanInforEx> page1 = page.map(batchPlanInforExMapper::toEntity);
            return page1;

    }

    @Override
    public void addBatchPlanInfor(BatchPlanInforDto batchPlanInforDto) {
            BatchPlanInfor result = batchPlanInforRepository.findBatchPlanInforByIdAndDelete(batchPlanInforDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("BatchPlanInfor不存在,请重新查询");
            }
            BatchPlanInfor batchPlanInfor = batchPlanInforMapper.toEntity(batchPlanInforDto);
            batchPlanInforRepository.save(batchPlanInfor);
    }

    @Override
    public void updateBatchPlanInfor(BatchPlanInforDto batchPlanInforDto) {
            BatchPlanInfor batchPlanInfor = batchPlanInforRepository.findBatchPlanInforById(batchPlanInforDto.getId());
            batchPlanInfor = batchPlanInforMapper.toEntity(batchPlanInforDto);
            batchPlanInforRepository.save(batchPlanInfor);
    }


    @Override
    public void delBatchPlanInfor(String id) {
        BatchPlanInfor batchPlanInfor = batchPlanInforRepository.findBatchPlanInforById(id);
        batchPlanInfor.setDelete(1);
        batchPlanInforRepository.save(batchPlanInfor);
    }

    @Override
    public void delBatchPlanInfors (List<String> ids) { }

    /**
     * 下发工单
     *
     * @param batchProductIds
     * @return
     */
    @Override
    public int submitBatchPlanInfor(List<String> batchProductIds) {
        return 0;
    }

//    /**
//     * 下发工单
//     *
//     * @param batchProductIds
//     * @return
//     */
//    @Override
//    public int submitBatchPlanInfor(List<String> batchProductIds) {
//        List<BatchProduct> batchProducts = batchProductRepository.findBatchProductsByIdInAndDelete(batchProductIds,0);
//        BatchPlanInforDetailDto batchPlanInforDetailDto = new BatchPlanInforDetailDto();
//        BatchPlan batchPlan = batchPlanRepository.findBatchPlanByIdAndDelete(batchProducts.get(0).getBatchPlanId(),0);
//
//        if (batchProducts.size() == 0) return -1;
//        // 下发的批次基本信息
//        batchPlanInforDetailDto =  bindbatchPlanInforBase(batchPlanInforDetailDto,batchProducts.get(0),batchPlan);
////        batchPlanInforDetailDto.setBatchPlanId(batchProducts.get(0).getBatchPlanId());
//        // 产品的列表
//        List<ProductDto> productDtos = new ArrayList<>();
//        ProductDto productDto = new ProductDto();
//        productDto.setProductNum(String.valueOf(batchProducts.size()));
////        productDto.setProductTypName(batchProducts);
//        List<WorkOrderInforDetailDto> workOrderInforDetailDtos = new ArrayList<>();
//        for (int i = 0; i < batchProducts.size(); i++) {
//            WorkOrderInforDetailDto workOrderInforDetailDto = new WorkOrderInforDetailDto();
//            BatchProduct batchProduct = batchProducts.get(i);
////            workOrderInforDto.setWo();  //自动生成
//            workOrderInforDetailDto.setBatchProductId(batchProduct.getId());
//            workOrderInforDetailDto.setBatchProductCode(batchProduct.getBatchPlanCode());
//            // 添加图纸信息
//            List<ProductDrawingDto> productDrawingDtos = new ArrayList<>();
//            List<ProductDrawingInfo> productDrawingInfos = productDrawingInfoRepository.findProductDrawingInfosByProductCodeAndDelete(batchProduct.getProductCode(),0);
//            for (ProductDrawingInfo productDrawingInfo : productDrawingInfos) {
//                ProductDrawingDto productDrawingDto = new ProductDrawingDto();
//                productDrawingDto.setDrawingId(productDrawingInfo.getId());
//                productDrawingDto.setDrawingCode(productDrawingInfo.getDrawingCode());
//                productDrawingDto.setDrawingSequence(productDrawingInfo.getDrawingSequence());
//                productDrawingDto.setDrawingUrl(productDrawingInfo.getDrawingUrl());
////                productDrawingDto.setDrawingDesc(productDrawingInfo.get());
//                productDrawingDtos.add(productDrawingDto);
//            }
//            workOrderInforDetailDto.setProductDrawings(productDrawingDtos);
//            // 添加BOM信息
//            List<ProductBomDetailDto> productBomDetailDtos = new ArrayList<>();
//            List<ProductBom> productBomDtos = productBomRepository.findProductBomByProductCodeAndDelete(batchProduct.getProductCode(),0);
//            for (ProductBom p:productBomDtos) {
//                ProductBomDetailDto productBomDetailDto = new ProductBomDetailDto();
//                productBomDetailDto.setBomId(p.getId());
//                productBomDetailDto.setBomCode(p.getBomCode());
////                productBomDetailDto.setProductCode(p.getProductCode());
//                List<ProductBomOverview> productBomOverviews = productBomOverviewRepository.findProductBomOverviewsByBomCodeAndDelete(p.getBomCode(),0);
//                List<ProductBomOverviewDto> productBomOverviewDtos = productBomOverviewMapper.toDto(productBomOverviews);
//                productBomDetailDto.setProductBomList(productBomOverviewDtos);
//            }
//            workOrderInforDetailDto.setProductBomDetailDtos(productBomDetailDtos);
//            // 加入工序信息
//
//
//            List<ProductManufactInforDto> productBomDetailDto = new ArrayList<>();
//
//
//
//
//            workOrderInforDetailDtos.add(workOrderInforDetailDto);
//        }
//        productDto.setProductInfors(workOrderInforDetailDtos);
//
////        workOrderInforDto.setProductSequence();
//
//
//        productDtos.add(productDto);
//        batchPlanInforDetailDto.setProductDtos(productDtos);
//        return 0;
//    }
//    /**
//     * 下发工单
//     *
//     * @param batchProductIds
//     * @return
//     */
//    @Override
//    public int submitBatchPlanInfor(List<String> batchProductIds) {
//        List<BatchProduct> batchProducts = batchProductRepository.findBatchProductsByIdInAndDelete(batchProductIds,0);
//        BatchPlanInfor batchPlanInfor = new BatchPlanInfor();
//        BatchPlan batchPlan = batchPlanRepository.findBatchPlanByIdAndDelete(batchProducts.get(0).getBatchPlanId(),0);
//
//        if (batchProducts.size() == 0) return -1;
//        // 下发的批次基本信息
//        batchPlanInfor =  bindbatchPlanInforBase(batchPlanInfor,batchProducts.get(0),batchPlan);
//        workOrderInforService.addWorkOrderInfors(batchProducts);
//        batchPlanInforRepository.save(batchPlanInfor);
//        // 产品的列表
////        List<ProductDto> productDtos = new ArrayList<>();
////        ProductDto productDto = new ProductDto();
////        productDto.setProductNum(String.valueOf(batchProducts.size()));
////        productDto.setProductTypName(batchProducts);
//        // 创建工单
////        List<WorkOrderInforDetailDto> workOrderInforDetailDtos = new ArrayList<>();
////        for (int i = 0; i < batchProducts.size(); i++) {
////            WorkOrderInforDetailDto workOrderInforDetailDto = new WorkOrderInforDetailDto();
////            BatchProduct batchProduct = batchProducts.get(i);
//////            workOrderInforDto.setWo();  //自动生成
////            workOrderInforDetailDto.setBatchProductId(batchProduct.getId());
////            workOrderInforDetailDto.setBatchProductCode(batchProduct.getBatchPlanCode());
////            // 添加图纸信息
////            List<ProductDrawingDto> productDrawingDtos = new ArrayList<>();
////            List<ProductDrawingInfo> productDrawingInfos = productDrawingInfoRepository.findProductDrawingInfosByProductCodeAndDelete(batchProduct.getProductCode(),0);
////            for (ProductDrawingInfo productDrawingInfo : productDrawingInfos) {
////                ProductDrawingDto productDrawingDto = new ProductDrawingDto();
////                productDrawingDto.setDrawingId(productDrawingInfo.getId());
////                productDrawingDto.setDrawingCode(productDrawingInfo.getDrawingCode());
////                productDrawingDto.setDrawingSequence(productDrawingInfo.getDrawingSequence());
////                productDrawingDto.setDrawingUrl(productDrawingInfo.getDrawingUrl());
//////                productDrawingDto.setDrawingDesc(productDrawingInfo.get());
////                productDrawingDtos.add(productDrawingDto);
////            }
////            workOrderInforDetailDto.setProductDrawings(productDrawingDtos);
////            // 添加BOM信息
////            List<ProductBomDetailDto> productBomDetailDtos = new ArrayList<>();
////            List<ProductBom> productBomDtos = productBomRepository.findProductBomByProductCodeAndDelete(batchProduct.getProductCode(),0);
////            for (ProductBom p:productBomDtos) {
////                ProductBomDetailDto productBomDetailDto = new ProductBomDetailDto();
////                productBomDetailDto.setBomId(p.getId());
////                productBomDetailDto.setBomCode(p.getBomCode());
//////                productBomDetailDto.setProductCode(p.getProductCode());
////                List<ProductBomOverview> productBomOverviews = productBomOverviewRepository.findProductBomOverviewsByBomCodeAndDelete(p.getBomCode(),0);
////                List<ProductBomOverviewDto> productBomOverviewDtos = productBomOverviewMapper.toDto(productBomOverviews);
////                productBomDetailDto.setProductBomList(productBomOverviewDtos);
////            }
////            workOrderInforDetailDto.setProductBomDetailDtos(productBomDetailDtos);
////            // 加入工序信息
////
////
////            List<ProductManufactInforDto> productBomDetailDto = new ArrayList<>();
////
//
//
////
////            workOrderInforDetailDtos.add(workOrderInforDetailDto);
////        }
////        productDto.setProductInfors(workOrderInforDetailDtos);
//
//
////        batchPlanInforRepository.save(batchPlanInfor);
//
//        return 0;
//    }

    // 填入基本的信息
//    public BatchPlanInfor bindbatchPlanInforBase(BatchPlanInfor batchPlanInfor,BatchProduct batchProduct,BatchPlan batchPlan) {
//        Info info = infoRepository.findInfoByIdAndDelete(batchProduct.getInfoId(),0);
//        batchPlanInfor.setInfoId(batchProduct.getInfoId());
//        batchPlanInfor.setBatchPlanId(batchProduct.getBatchPlanId());
//        batchPlanInfor.setBatchPlanCode(batchProduct.getBatchPlanCode());
//        batchPlanInfor.setPlanStartTime(batchPlan.getPlanOrderStartDate());
//        batchPlanInfor.setPlanEndTime(batchPlan.getPlanOrderEndDate());
//        batchPlanInfor.setProjectName(info.getProjectName());
//        batchPlanInfor.setProjectCode(info.getProjectCode());
//
//        return batchPlanInfor;
//    }



    // 填入基本的信息
//    public BatchPlanInforDetailDto bindbatchPlanInforBase(BatchPlanInforDetailDto batchPlanInforDetailDto,BatchProduct batchProduct,BatchPlan batchPlan) {
//        Info info = infoRepository.findInfoByIdAndDelete(batchProduct.getInfoId(),0);
//        batchPlanInforDetailDto.setInfoId(batchProduct.getInfoId());
//        batchPlanInforDetailDto.setBatchPlanId(batchProduct.getBatchPlanId());
//        batchPlanInforDetailDto.setBatchPlanCode(batchProduct.getBatchPlanCode());
//        batchPlanInforDetailDto.setPlanStartTime(batchPlan.getPlanOrderStartDate());
//        batchPlanInforDetailDto.setPlanEndTime(batchPlan.getPlanOrderEndDate());
//        batchPlanInforDetailDto.setProjectName(info.getProjectName());
//        batchPlanInforDetailDto.setProjectCode(info.getProjectCode());
//
//        return batchPlanInforDetailDto;
//    }


}